package se.perp.c4.ui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import se.perp.c4.control.iface.IPlacementButtonListener;
import se.perp.c4.model.Actor.EActor;
import se.perp.c4.model.Territory.ETerritory;
import static se.perp.c4.model.Territory.ETerritory.*;

public class PlacementPanel extends JPanel implements ActionListener {

    private PieceCollection pieceColl;
    private BufferedImage bgImage;
    private ActorViewer actorViewer;
    private ETerritory[] validTerritories;
    private Color homePolyColor = new Color(0, 0, 255, 168);
    private Color polyColor = new Color(0, 255, 0, 168);
    private JButton okButton,  resetButton,  helpButton;
    private IPlacementButtonListener listener = null;

    private JButton randomButton;

    public PlacementPanel(
            MainWindow mw,
            PieceCollection pc, ActorViewer av,
            EActor[] validActors, ETerritory[] validTerritories) {
        pieceColl = pc;
        actorViewer = av;
        this.validTerritories = validTerritories;

        bgImage = mw.getBackgroundImage();

        for (GamePiece p : pieceColl) {
            p.setVisible(false);
            for (EActor ea : validActors) {
                if (p.getActor() == ea) {
                    add(p);
                    p.setVisible(true);
                }
            }
        }

        setLayout(null);
        setSize(bgImage.getWidth(), bgImage.getHeight());
        setPreferredSize(getSize());

        add(actorViewer);
        actorViewer.setVisible(true);

        helpButton = new JButton("Help");
        helpButton.setMnemonic('H');
        helpButton.addActionListener(this);
        helpButton.setToolTipText("Get help on character placement.");

        okButton = new JButton("OK");
        okButton.setMnemonic('O');
        okButton.addActionListener(this);
        okButton.setToolTipText("Accepts the current placement.");

        resetButton = new JButton("Reset");
        resetButton.setMnemonic('R');
        resetButton.addActionListener(this);
        resetButton.setToolTipText("Removes all pieces from the board.");

        randomButton = new JButton("Rand");
        randomButton.setMnemonic('a');
        randomButton.addActionListener(this);
        randomButton.setToolTipText("Randomizes placement.");

        ButtonPanel bp = new ButtonPanel();
        bp.setSize(200, 25);
        bp.setLocation(getWidth() - bp.getWidth()-3, getHeight() - bp.getHeight()-3);
        bp.setLayout(new GridLayout(1,3,5,0));
        bp.add(okButton);
        bp.add(resetButton);
        bp.add(helpButton);
//        bp.add(randomButton);

        add(bp);

    }

    public JButton getOkButton() {
        return okButton;
    }

    public JButton getResetButton() {
        return resetButton;
    }

    public void setPlacementButtonListener(IPlacementButtonListener l) {
        listener = l;
    }

    public void actionPerformed(ActionEvent e) {
        JButton source = (JButton) e.getSource();
        if (source == okButton && listener != null) {
            listener.okClicked();
        } else if (source == resetButton && listener != null) {
            listener.resetClicked();
        } else if (source == helpButton) {
            new HelpWindow("Character placement", "/doc/character_placement.html");
        } else if (source == randomButton) {
            listener.randomClicked();
        }
    }

    private class ButtonPanel extends JPanel {

        public ButtonPanel() {
            setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle bounds = g.getClipBounds();
            Rectangle r = SwingUtilities.convertRectangle(this, bounds, getParent());
            Image subImg = bgImage.getSubimage(r.x, r.y, r.width, r.height);
            g.drawImage(subImg, bounds.x, bounds.y, null);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        Rectangle r = g.getClipBounds();
        Image subImg = bgImage.getSubimage(r.x, r.y, r.width, r.height);
        g.drawImage(subImg, r.x, r.y, null);

        for (ETerritory et : validTerritories) {
            Polygon poly = RegionBounds.get(et);
            Color c;
            if (et == SHIRE || et == MORDOR) {
                c = homePolyColor;
            } else {
                c = polyColor;
            }

            g.setColor(c);
            g.fillPolygon(poly);

        }
    }
}
